import Vue from "vue";
import Vuex from "vuex";
import web3 from "@/lib/contract/web3";

Vue.use(Vuex);

const SET_WALLET_ADDRESS = "SET_WALLET_ADDRESS";

export default new Vuex.Store({
  state: {
    walletAddress: ""
  },
  mutations: {
    [SET_WALLET_ADDRESS](state, walletAddress) {
      state.walletAddress = walletAddress;
    }
  },
  actions: {
    async connectMetaMask({ commit }) {
      await web3.connectMetaMask();
      await web3.connectWallet();
      const walletAddress = web3.getCurrWalletAddress();

      commit(SET_WALLET_ADDRESS, walletAddress);
    }
  },
  getters: {
    walletAddress: state => state.walletAddress
  },
  modules: {}
});
